﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ERF;
using ERF.Menu;
using FirstPerson.Objects;

namespace FirstPerson.States
{
    class StartMenuState : State
    {
        /// <summary>
        /// Singleton instance
        /// </summary>
        private static StartMenuState instance;

        private FireSkull skull;
        private Camera camera;
        private MenuManager menuManager;
        private Menu menu;
        private SceneGraph sceneGraph;
        private Song song;

        /// <summary>
        /// Get an instance of this state.
        /// </summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        public static StartMenuState GetInstance(StateMachine machine)
        {
            if (instance == null)
                instance = new StartMenuState(machine);

            return instance;
        }

        private StartMenuState(StateMachine machine)
            : base(machine)
        {
            this.machine.CurrentGame.Window.Title = "Battlesquadron project";

            this.sceneGraph = new SceneGraph(machine.CurrentGame);

            this.skull = new FireSkull(base.machine.CurrentGame);
            this.skull.Position = new Vector3(6, -3, -20);
            this.skull.Rotation = new Vector3(0, -(MathHelper.PiOver4 / 1f), 0);

            this.sceneGraph.Items.Add(this.skull);

            this.camera = new Camera(
                base.machine.CurrentGame,
                MathHelper.PiOver4,
                base.machine.CurrentGame.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                1000f,
                Vector3.Zero,
                Vector2.Zero,
                Vector3.Up);

            menuManager = MenuManager.GetInstance(this.machine.CurrentGame);

            menu = new Menu("BattleSqudron project");
            menu.MenuItems.Add(new MenuItem("Host Game", this.GotoStateHostGame));
            menu.MenuItems.Add(new MenuItem("Join Game", this.GotoStateJoinGame));
            menu.MenuItems.Add(new MenuItem("Options", this.GotoStateOptions));
            menu.MenuItems.Add(new MenuItem("Quit", this.QuitGame));
            menu.SelectedIndex = 0;

            this.song = this.machine.CurrentGame.Content.Load<Song>("Sounds\\MenuMusic");
        }

        /// <summary>
        /// Initialize an instant of StartMenuState with a Menu named BattleSqudron project
        /// </summary>
        public override void Initialize()
        {
            // If we aren't already playing the menu music, start playing it!
            if (MediaPlayer.State != MediaState.Playing)
            {
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(song);
                MediaPlayer.Volume = 0.2f;
            }

            //this.skull.Position = Vector3.Zero;
            //this.camera.Position = new Vector3(0, 0, 10);
            //this.camera.Rotation = Vector2.Zero;

            base.machine.CurrentGame.CurrentSceneGraph = this.sceneGraph;
            base.machine.CurrentGame.Camera = this.camera;

            menuManager.Menu = menu;
        }

        /// <summary>
        /// Push an instant of HostGameState 
        /// </summary>
        private void GotoStateHostGame()
        {
            machine.CurrentState = HostState.GetInstance(machine);
        }

        /// <summary>
        /// Push an instant of JoinGameState
        /// </summary>
        private void GotoStateJoinGame()
        {
            machine.CurrentState = JoinState.GetInstance(machine);
        }

        /// <summary>
        /// Push an instant of OptionsMenuState 
        /// </summary>
        private void GotoStateOptions()
        {
            machine.CurrentState = OptionsState.GetInstance(machine);
        }

        /// <summary>
        /// Exit the game
        /// </summary>
        private void QuitGame()
        {
            this.machine.CurrentGame.Exit();
        }

        /// <summary>
        /// Get input from user and submit the choice of the menu 
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (input.IsButtonReleased("MENU_BACK"))
                QuitGame();

            if (input.IsButtonReleased("MENU_DOWN"))
            {
                menuManager.Menu.SelectNext();
            }

            if (input.IsButtonReleased("MENU_UP"))
            {
                menuManager.Menu.SelectPrevious();
            }

            if (input.IsButtonReleased("MENU_SELECT"))
            {
                menuManager.Menu.Submit();
            }
        }
    }
}
